package com.course.app.api.controller;

import com.course.app.api.data.*;
import com.course.app.biz.*;
import com.course.app.transformer.StudentTransformer;
import com.course.domain.model.Account;
import com.course.domain.model.Student;
import com.course.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;

/**
 * Created by Justin on 2017/5/8.
 */
@RestController
@PreAuthorize("hasRole('STUDENT')")
@RequestMapping("/api/students")
public class StudentController {

    private final UserBiz userBiz;
    private final MarkBiz markBiz;
    private final LearnBiz learnBiz;
    private final CourseBiz courseBiz;
    private final AgendaBiz agendaBiz;

    @Autowired
    public StudentController(UserBiz userBiz,
                             MarkBiz markBiz,
                             LearnBiz learnBiz,
                             CourseBiz courseBiz,
                             AgendaBiz agendaBiz) {
        this.userBiz = userBiz;
        this.markBiz = markBiz;
        this.learnBiz = learnBiz;
        this.courseBiz = courseBiz;
        this.agendaBiz = agendaBiz;
    }

    @GetMapping("/{studentId}")
    public StudentDTO findStudentByStudentId(@PathVariable int studentId) {
        Student student = userBiz.loadStudent(studentId);
        if (student == null) {
            return null;
        }
        Account account = userBiz.loadAccount(student.getAccountId());
        if (account == null) {
            return null;
        }
        int courseId = student.getCourseId();
        CourseDTO courseDTO = courseBiz.loadCourse(studentId, courseId);
        if (courseDTO == null) {
            return null;
        }
        StudentDTO studentDTO = StudentTransformer.toDTO(student, account);
        studentDTO.setCourse(courseDTO);
        return studentDTO;
    }

    @GetMapping("/{studentId}/agenda")
    public AgendaDTO findAgendaByStudentId(@PathVariable int studentId,
                                           @RequestParam(value = "date", required = false)
                                           @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        Date from = date != null ? DateUtils.toDate(date) : DateUtils.now();
        return agendaBiz.findAgendaByStudentId(studentId, from);
    }

    @GetMapping("/{studentId}/review")
    public MarkDTO findReviewByStudentId(@PathVariable int studentId,
                                         @PageableDefault(sort = {"date"}) Pageable pageable) {
        return markBiz.findMarkByStudent(studentId, pageable);
    }

    @PostMapping("/{studentId}/score")
    public String addScoreByStudentId(@PathVariable int studentId) {
        Student student = userBiz.loadStudent(studentId);
        if (student == null) {
            return "学生不存在";
        }
        student.setScore(student.getScore() + 1);
        userBiz.updateStudent(student);
        return "OK";
    }

    @GetMapping("/{studentId}/record")
    public LearnStatDTO findLearnByStudentId(@PathVariable int studentId,
                                             @RequestParam(value = "date", required = false)
                                             @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        LocalDate today = date != null ? date : DateUtils.toLocalDate(DateUtils.now());
        return learnBiz.loadStat(studentId, today);
    }

    @PostMapping("/{studentId}/record/{groupId}")
    public LearnStatDTO recordLearnByStudentId(@PathVariable int studentId,
                                               @PathVariable int groupId,
                                               @RequestParam(value = "date", required = false)
                                               @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        learnBiz.record(studentId, groupId);
        LocalDate today = date != null ? date : DateUtils.toLocalDate(DateUtils.now());
        return learnBiz.loadStat(studentId, today);
    }
}
